<template>
    <div class='wrap'>
      <div class="rule-main">
        <div class="rule-contain">
            <transition name="slide-fade">
              <div v-show="showRule" class="rule">
                <div v-html="bgContent"></div>
                <div v-html="ruleContent"></div>
              </div>
            </transition>
            <!--div class="rule-phone" v-if="hasPhoneNumber">
              参赛手机：{{phoneNumber}}
            </div>
            <div class="rule-phone" v-else>
              <input v-model="phoneNumber" placeholder="输入参赛手机号码" type="tel" maxlength="11" style="text"/>
            </div-->
        </div>
        <div id="btn_h" @click='goto'></div>
      </div>

    </div>
</template>

<script>
export default {
  data () {
    return {
      bgContent: '',
      ruleContent: '',
      phoneNumber: null,
      hasPhoneNumber: false,
      showRule: false
    }
  },
  methods: {
    goto () {
      // if (this.phoneNumber === null || this.phoneNumber.length < 11) {
      //   alert('请正确填写手机号码')
      // } else {
      //   localStorage.setItem('tel', this.phoneNumber)
      //   this.GLOBAL.player.openId = this.phoneNumber
      this.$router.push('/mode')
      // }
    },
    loadBg () {
      this.$http.getBackground().then(
        (result) => {
          this.bgContent = '<p style="margin-top:0">故事背景</p>' +
            result.data.replace(new RegExp('\\n', 'gm'), '<br/>')
        }
      ).catch((error) => {
        console.error(error)
      })
    },
    loadRule () {
      this.$http.getRule().then(
        (result) => {
          this.ruleContent = '<p>游戏规则</p>' +
            result.data.replace(new RegExp('\\n', 'gm'), '<br/>')
        }
      ).catch((error) => {
        console.error(error)
      })
    },
    hasTel () {
      let phoneNumber = localStorage.getItem('tel')
      if (phoneNumber) {
        this.phoneNumber = phoneNumber
        this.hasPhoneNumber = true
      }
    }
  },
  beforeCreate () {
    // this.GLOBAL.validate()
  },
  created () {
    this.loadBg()
  },
  mounted: function () {
    this.loadRule()
    this.hasTel()
    document.getElementsByTagName('body')[0].className = 'bgimg'
    // 添加返回事件监听
    let that = this
    history.pushState(null, null, '')
    window.onpopstate = function (event) {
      that.$router.push({path: '/'})
    }
    this.showRule = true
  },
  beforeDestroy: function () {
    document.body.removeAttribute('class', 'bgimg')
  }
}
</script>

<style>
.bgimg {
  background:url('../assets/home/bg.png') no-repeat;
  background-size:cover;
}
.wrap {
  overflow: hidden;
  width: 100%;
  display: flex;
  justify-content: center;
}
.rule-main {
  margin-top: 1rem;
  display: flex;
  flex-direction: column;
  background:url('../assets/rule/bg_1.png') no-repeat;
  background-size:20.5rem;
  min-height: 36rem;
  width: 20.5rem;
  align-items: center;
}
.rule-main #btn_h {
  height: 3.5rem;
  width: 12rem;
  background:url('../assets/rule/btn_1.png') no-repeat;
  background-size:12rem 3.5rem;
}
.rule-contain {
  margin-bottom: 1.5rem;
  width: 15rem;
}
.rule {
  width: 15rem;
  height:20rem;
  color: white;
  font-size: 0.8rem;
  overflow-y:scroll;
  -webkit-overflow-scrolling:touch;
  overflow: hidden;
  margin-top: 4rem;
}
.rule p {
  text-align: center;
  font-size: 0.9rem;
}
.rule-phone {
  text-align: center;
  width: 100%;
  height: 2rem;
  border-radius: 0.3rem;
  background-color:white;
  opacity: 0.8;
  color: black;
  font-size: 1rem;
}
.rule-phone input {
    margin: 0.3rem auto;
    font-size: 1rem;
    border: 0px;
    text-align: center;
}
.slide-fade-enter-active {
  transition: all 2s ease;
}
.slide-fade-enter
/* .slide-fade-leave-active for below version 2.1.8 */ {
  transform: translateX(-2rem);
  opacity: 0;
}
</style>
